package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ProgressStatusForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.EAudio;
import com.jilefojing.beans.query.EAudioQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IEAudioDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IEAudioMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月19日 13时11分12秒
 */
@Repository
public class EAudioDao extends BaseDao<EAudio> implements IEAudioDao {
	@Autowired
	private IEAudioMapper audioMapper;

	@Override
	public Pagination<EAudio> getPagination(EAudioQuery eAudioQuery, Pagination<EAudio> pagination) {
		Example example = new Example(EAudio.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(eAudioQuery.getIds())) {
			criteria.andIn(EAudio.FIELD_ID, eAudioQuery.getIds());
		}

		if (Detect.notEmpty(eAudioQuery.getParentIds())) {
			criteria.andIn(EAudio.FIELD_PARENTID, eAudioQuery.getParentIds());
		}

		if (eAudioQuery.getEntity() != null) {
			if (eAudioQuery.getEntity()) {
				criteria.andEqualTo(EAudio.FIELD_ENTITY, 1);
			} else {
				criteria.andEqualTo(EAudio.FIELD_ENTITY, 0);
			}
		}

		if (Detect.notEmpty(eAudioQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForPublish _Status : eAudioQuery.getStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(EAudio.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(eAudioQuery.getProgressStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (ProgressStatusForSource _Status : eAudioQuery.getProgressStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(EAudio.FIELD_PROGRESSSTATUS, statusCodes);
		}

		if (Detect.notEmpty(eAudioQuery.getTitle())) {
			criteria.andEqualTo(EAudio.FIELD_TITLE, eAudioQuery.getTitle());
		}

		if (Detect.notEmpty(eAudioQuery.getLike_title())) {
			criteria.andLike(EAudio.FIELD_TITLE, "%" + eAudioQuery.getLike_title() + "%");
		}

		if (Detect.notEmpty(eAudioQuery.getTypes())) {
			criteria.andCondition(this.getTypesCondition(eAudioQuery.getTypes()));
		}

		if (Detect.notEmpty(eAudioQuery.getResolutions())) {
			criteria.andCondition(this.getResolutionsCondition(eAudioQuery.getResolutions()));
		}

		if (eAudioQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(EAudio.FIELD_CREATETIME, eAudioQuery.getEqualCreateTime());
		}

		if (eAudioQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EAudio.FIELD_CREATETIME, eAudioQuery.getEqualAndBeforCreateTime());
		}

		if (eAudioQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(EAudio.FIELD_CREATETIME, eAudioQuery.getEqualAndAfterCreateTime());
		}

		if (eAudioQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(EAudio.FIELD_CREATETIME, eAudioQuery.getBeforCreateTime());
		}

		if (eAudioQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(EAudio.FIELD_CREATETIME, eAudioQuery.getAfterCreateTime());
		}

		if (eAudioQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(EAudio.FIELD_UPDATETIME, eAudioQuery.getEqualUpdateTime());
		}

		if (eAudioQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EAudio.FIELD_UPDATETIME, eAudioQuery.getEqualAndBeforUpdateTime());
		}

		if (eAudioQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(EAudio.FIELD_UPDATETIME, eAudioQuery.getEqualAndAfterUpdateTime());
		}

		if (eAudioQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(EAudio.FIELD_UPDATETIME, eAudioQuery.getBeforUpdateTime());
		}

		if (eAudioQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(EAudio.FIELD_UPDATETIME, eAudioQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(eAudioQuery.getCreateUserIds())) {
			criteria.andIn(EAudio.FIELD_CREATEUSER, eAudioQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}

	private String getResolutionsCondition(List<String> resolutions) {
		return this.getTypesCondition(resolutions);
	}

	private String getTypesCondition(List<String> types) {
		// and (so like '' or so like )
		StringBuilder typesBuilder = new StringBuilder();
		typesBuilder.append("( ");
		for (int i = 0; i < types.size(); i++) {
			String type = types.get(i);
			typesBuilder.append(EAudio.COLUMN_SOURCES).append(" like ").append("'%").append(type).append("%'");
			if (i != types.size() - 1) {
				typesBuilder.append(" or ");
			}
		}
		typesBuilder.append(" ) ");
		return typesBuilder.toString();
	}

	//
	// public static void main(String[] args) {
	// List<String> types = new ArrayList<String>();
	// types.add("mp4");
	// // types.add("avi");
	// EAudioDao eAudioDao = new EAudioDao();
	// System.out.println(eAudioDao.getTypesCondition(types));
	// }

	@Override
	public List<EAudio> getListByIds(List<Long> ids) {
		Example example = new Example(EAudio.class);
		example.createCriteria().andIn(EAudio.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public List<EAudio> getListByParentId(long parentId) {
		return this.getListByParentIds(Arrays.asList(new Long[] { parentId }));
	}

	@Override
	public List<EAudio> getListByParentIds(List<Long> parentIds) {
		Example example = new Example(EAudio.class);
		example.createCriteria().andIn(EAudio.FIELD_PARENTID, parentIds);
		example.setOrderByClause(EAudio.ORDER_SORT_DESC);
		return super.getListByExample(example);
	}

	@Override
	public List<Map<String, Integer>> countCognatesByParentIds(List<Long> parentIds) {
		return audioMapper.countCognatesByParentIds(parentIds);
	}

	@Override
	public List<Map<String, Integer>> countCognatePlaytimeByParentIds(List<Long> parentIds) {
		return audioMapper.countCognatePlaytimeByParentIds(parentIds);
	}
}
